package com.easthome.service.impl;

import com.easthome.constant.Constants;
import com.easthome.entity.Goods;
import com.easthome.mapper.GoodsMapper;
import com.easthome.pojo.form.GoodsPageConForm;
import com.easthome.pojo.vo.GoodsVO;
import com.easthome.service.GoodsService;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author cxiaoy
 * @Date 2022/10/14 14:35
 */
@Service
public class GoodsServiceImpl implements GoodsService {

    @Autowired
    private GoodsMapper goodsMapper;

    @Override
    public List<GoodsVO> goodsList() {
        return goodsMapper.goodsList();
    }

    @Override
    public Goods goodsById(Integer id) {
        return goodsMapper.goodsById(id);
    }

    @Override
    public int updateGoods(Goods goods) {
        return goodsMapper.updateGoods(goods);
    }

    @Override
    public int addGoods(Goods goods) {
        return goodsMapper.addGoods(goods);
    }

    @Override
    public int deleteGoods(Integer goodsId) {
        return goodsMapper.deleteGoods(goodsId);
    }

    @Override
    public int deleteBatch(List<Integer> ids) {
        return goodsMapper.deleteBatch(ids);
    }

    @Override
    public List<Goods> getGoodsList() {
        return goodsMapper.getGoodsList();
    }

    /*
    分页:
    PageHelper原理上是个拦截器，当它发现离它最近的那条sql要执行，它先接管
    select count(*) from (我们的sql) as
    我们的sql limit
     */
    @Override
    public PageInfo getGoodsListPage(Integer pageNum) {
        Page p = PageHelper.startPage(pageNum, Constants.PAGE_SIZE);//分页插件
        List<Goods> goodsList = goodsMapper.getGoodsList();//我们自己的sql

        PageInfo pageInfo = new PageInfo(goodsList);
        return pageInfo;
    }

    @Override
    public PageInfo getGoodsListPageCon(Integer pageNum, GoodsPageConForm goodsPageConForm) {
        PageHelper.startPage(pageNum,Constants.PAGE_SIZE);
        List<Goods> list = goodsMapper.getGoodsListPageCon(goodsPageConForm);
//        PageInfo p = new PageInfo(list);
        return new PageInfo(list);
    }
}
